package com.Ivy.updDemo;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.util.Scanner;

public class ServerThread extends Thread{
    private DatagramSocket ssocket;

    public ServerThread() {
        super("小明：");
    }

    @Override
    public void run() {
        try {
            ssocket=new DatagramSocket(9997);
            byte[] bt=new byte[1024];
            DatagramPacket sdp=new DatagramPacket(bt,bt.length);
            while (true){
                ///////////////////接收信息////////////////////
                ssocket.receive(sdp);
                int len=sdp.getLength();
                String cSendSmg=new String(bt,0,len);
                System.out.println(cSendSmg);
                //////////////////////发送消息/////////////////////
                Scanner sc=new Scanner(System.in);
                String strClientMsg=Thread.currentThread().getName();
                strClientMsg+=sc.next();
                DatagramPacket cdg=new DatagramPacket(strClientMsg.getBytes(),0,strClientMsg.getBytes().length,
                        sdp.getSocketAddress());
                System.out.println(strClientMsg);
                ssocket.send(cdg);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

    }
}
